from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.filters import BaseFilterBackend
from . import models


class MyFilterBackend(BaseFilterBackend):

    def filter_queryset(self, request, queryset, view):
        val = request.query_params.get('category')
        return queryset.filter(category_id=val)


class IndexView(APIView):

    def get(self, request, *args, **kwargs):

        queryset = models.News.objects.all()
        obj = MyFilterBackend()
        result = obj.filter_queryset(request, queryset, self)
        print(result)
        return Response('lll')


from rest_framework.generics import GenericAPIView, CreateAPIView, ListAPIView, RetrieveAPIView, DestroyAPIView, UpdateAPIView
from rest_framework import serializers
from rest_framework.pagination import PageNumberPagination


class NewFilterBackend(BaseFilterBackend):

    def filter_queryset(self, request, queryset, view):
        val = request.query_params.get('category')
        return queryset.filter(category_id=val)


class NewSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.News
        fields = '__all__'


'''
class NewsView(GenericAPIView):
    queryset = models.News.objects.all()
    filter_backends = [NewFilterBackend,]
    serializer_class = NewSerializer
    pagination_class = PageNumberPagination

    def get(self, request, *args, **kwargs):
        v= self.get_queryset()
        queryset = self.filter_queryset(v)
        data = self.paginate_queryset(queryset)

        ser = self.get_serializer(instance=data, many=True)
        return Response(ser.data)
'''


class NewsView(ListAPIView):
    queryset = models.News.objects.all()
    filter_backends = [NewFilterBackend,]
    serializer_class = NewSerializer
    pagination_class = PageNumberPagination


class TagSer(serializers.ModelSerializer):
    class Meta:
        model = models.Tag
        fields = '__all__'


class TagView(ListAPIView, CreateAPIView):
    queryset = models.Tag.objects.all()
    serializer_class = TagSer


class TagDetailView(RetrieveAPIView, UpdateAPIView, DestroyAPIView):
    queryset = models.Tag.objects.all()
    serializer_class = TagSer